package com.qg.cyapp.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.qg.cyapp.R;
import com.qg.cyapp.bean.Item;
import com.qg.cyapp.bean.MealTimesBean;
import com.qg.cyapp.bean.Order;
import com.qg.cyapp.component.Dolly;
import com.qg.cyapp.util.ShareUtil;
import com.qg.cyapp.util.ToastUtil;

public class ShoppingDollyActivity extends HttpActivity {
	// private String[] choice = { "餐到付款", "使用账户余额", "在线支付", "刷饭卡" };

	private Dolly dolly;
	private TextView fanhui, tijiao, zongjia;
	private ListView lv;
	private List<Item> il; // 购物清单
	public int wh = 0;

	private OnFocusChangeListener ofl = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (!hasFocus) {
				String s = ((EditText) v).getText().toString();
				int i = Integer.valueOf(s);
				if (i < 1) {
					Toast.makeText(ShoppingDollyActivity.this, "请输入正确的数量",
							Toast.LENGTH_SHORT).show();
					v.setFocusable(true);
					v.setFocusableInTouchMode(true);
					v.requestFocus();
				} else {
					Holder h = (Holder) v.getTag();
					h.item.setNumber(i);
					ba.notifyDataSetChanged();
				}
			}
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			int position = data.getIntExtra(ShareUtil.DATA, -1);
			MealTimesBean mealTimes = ChooseMealTimeActivity.mealTimes
					.get(position);
			String date = data.getStringExtra(ShareUtil.DATE);
			Order order = new Order();
			order.mealTimesId = mealTimes.mealTimesId;
			order.mealTimesName = mealTimes.mealTimesName;
			order.sum = Dolly.getDolly().getTotalPrice();
			order.time = date;
			order.items = il;
			uploadOrder(order);
			ToastUtil.show(this, "订餐成功");
			finish();
		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_shopping_dolly;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		if (Dolly.isEnpty()) {
			this.finish();
		}
		dolly = Dolly.getDolly();
		il = dolly.getFinalDolly();
		fanhui = (TextView) mfindViewAndSetOnClick(R.id.tv_go_back);
		tijiao = (TextView) mfindViewAndSetOnClick(R.id.tv_submit);
		zongjia = (TextView) findViewById(R.id.tv_zongjia);
		lv = (ListView) findViewById(R.id.lv_items);
		lv.setAdapter(ba);
		zongjia.setText(dolly.getTotalPrice() + "元");

	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		Holder h = (Holder) view.getTag();
		switch (view.getId()) {
		case R.id.tv_go_back:
			ShoppingDollyActivity.this.finish();
			break;
		case R.id.tv_submit:
			if (il.isEmpty()) {
				Toast.makeText(ShoppingDollyActivity.this, "你还没选择任何食物呢",
						Toast.LENGTH_LONG).show();
				return;
			}
			Intent intent = new Intent(ShoppingDollyActivity.this,
					ChooseMealTimeActivity.class);
			startActivityForResult(intent, 0);
			break;
		case R.id.tv_jia:
			h.item.addNumber();
			ba.notifyDataSetChanged();
			break;
		case R.id.tv_jian:
			if (h.item.number == 1)
				return;
			h.item.cutNumber();
			ba.notifyDataSetChanged();
			break;
		case R.id.tv_shan:
			dolly.deleteFood(h.item.food.id);
			zongjia.setText(dolly.getTotalPrice() + "元");
			ba.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

	private void uploadOrder(Order order) {
		// TODO Auto-generated method stub
		params.clear();
		params.put("oldersNo", ShareUtil.olderId);
		params.put("Order", gson.toJson(order));
		params.put("action", "putOrder");
		addPostRequest("OrderAction", params, 0);
		sendPostRequest();
	}

	BaseAdapter ba = new BaseAdapter() {

		@Override
		public int getCount() {
			return il.size();
		}

		@Override
		public Object getItem(int position) {
			return il.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = ShoppingDollyActivity.this.getLayoutInflater()
						.inflate(R.layout.item_dolly, parent, false);
				Holder h = new Holder();

				h.caiming = (TextView) convertView
						.findViewById(R.id.tv_caiming);
				h.danjia = (TextView) convertView.findViewById(R.id.tv_danjia);
				h.jia = (TextView) convertView.findViewById(R.id.tv_jia);
				h.jian = (TextView) convertView.findViewById(R.id.tv_jian);
				h.shan = (TextView) convertView.findViewById(R.id.tv_shan);
				h.xiaoji = (TextView) convertView.findViewById(R.id.tv_xiaoji);
				h.shuliang = (EditText) convertView
						.findViewById(R.id.et_shuliang);

				mFindViewAndSetOnClick(h.jia);
				mFindViewAndSetOnClick(h.jian);
				mFindViewAndSetOnClick(h.shan);
				h.shuliang.setOnFocusChangeListener(ofl);

				convertView.setTag(h);
				h.jia.setTag(h);
				h.jian.setTag(h);
				h.shan.setTag(h);
				h.shuliang.setTag(h);
			}

			Holder h = (Holder) convertView.getTag();
			h.item = il.get(position);
			h.caiming.setText(h.item.food.name);
			h.danjia.setText(h.item.food.price + "元");
			h.xiaoji.setText(h.item.getTotalPrice() + "元");
			h.shuliang.setText(String.valueOf(h.item.number));
			zongjia.setText(dolly.getTotalPrice() + "元");
			return convertView;
		}
	};

	class Holder {
		public TextView jia, jian, shan, caiming, danjia, xiaoji;
		public EditText shuliang;
		public Item item;
	}

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub

	}
}
